﻿using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataService.Office.Word
{
    /// <summary>
    /// 处理word代码域
    /// </summary>
    public class XmlPackageFldchar
    {
        /// <summary>
        /// 返回域代码
        /// </summary>
        /// <param name="openXmlElement"></param>
        /// <returns></returns>
        public static List<string> ReturnWordYuCode(OpenXmlElement openXmlElement)
        {
            var FieldCodeList = openXmlElement.Descendants<FieldCode>();
            return FieldCodeList.Select(x => x.Text).ToList();
        }
        /// <summary>
        /// 更新此节点下所有域
        /// xml结构如下，每个域都是由标签<fldcar  fldchartype=begin>开始,<instrText>记录域代码
        /// <fldcar  fldchartype=separate>  后面的<text>展示域结果（此部分的作用是决定该域在word中展示的最终结果，可以修改这部分值，达到不需要在word中F9刷新更新域）
        /// <fldcar  fldchartype=end>结束当前域
        /// </summary>
        /// <param name="me">要更新的文档节点 如document、body等</param>
        /// <param name="values">关于域信息的键值对</param>
        public static void UpdateFileds(OpenXmlElement me, Dictionary<string, string> values)
        {
            if (me.HasChildren)
            {
                IEnumerable<OpenXmlElement> childrens = me.ChildElements;
                int count = 0;
                bool hasFileds = false;
                string FieldCode = "";
                int mark = 0;
                foreach (OpenXmlElement itemOpenXmlElement in childrens)
                {
                    count++;
                    var itemChildElements = itemOpenXmlElement.ChildElements;
                    if (itemChildElements.Count == 0)
                        continue;
                    if (itemChildElements.OfType<FieldChar>().FirstOrDefault(p => p.FieldCharType == FieldCharValues.Begin) != null)
                    {
                        hasFileds = true;
                        continue;
                    }
                    if (hasFileds)
                    {
                        if (itemChildElements.OfType<FieldChar>().FirstOrDefault(p => p.FieldCharType == FieldCharValues.End) != null)
                        {
                            hasFileds = false;
                            FieldCode = "";
                            continue;
                        }

                        if (itemChildElements.OfType<FieldChar>().FirstOrDefault(p => p.FieldCharType == FieldCharValues.Separate) != null)
                        {
                            mark = count + 1;
                            continue;
                        }

                        if (mark == count && itemChildElements.OfType<Text>().FirstOrDefault() != null)
                        {
                            string value = "";
                            values.TryGetValue(FieldCode, out value);
                            Text text1 = new Text();
                            text1.Text = value;
                            itemOpenXmlElement.RemoveAllChildren();
                            itemOpenXmlElement.Append(text1);
                            continue;
                        }
                        var instrText = itemOpenXmlElement.ChildElements.OfType<FieldCode>().FirstOrDefault();
                        if (instrText != null)
                        {
                            foreach (string key in values.Keys)
                            {
                                if (instrText.Text.IndexOf(" " + key + " ") == -1)
                                {
                                    FieldCode = "";
                                }
                                else
                                {
                                    FieldCode = key;
                                }
                            }
                        }
                    }
                    else
                    {
                        UpdateFileds(itemOpenXmlElement, values);
                    }

                }

            }
        }
    }
}
